package com.hust.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hust.dao.CommodityDao;
import com.hust.dao.CommodityExample;
import com.hust.dao.PriceDao;
import com.hust.dao.PriceExample;
import com.hust.domain.Commodity;
import com.hust.domain.PageResult;
import com.hust.domain.Price;
import com.hust.exceptions.ClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class CommodityService {

    @Autowired
    private CommodityDao commodityDao;

    @Autowired
    private PriceDao priceDao;

   public PageInfo findHistoryByUrl(int pageSize,String url){
       //准备Commodity的查询条件
       CommodityExample ce_commodity=new CommodityExample();
       CommodityExample.Criteria criteria_commodity= ce_commodity.createCriteria();
       criteria_commodity.andCUrlIsNotNull();
       criteria_commodity.andCUrlEqualTo(url);
       //根据url查询
       List<Commodity> commodities = commodityDao.selectByExample(ce_commodity);
       //如果commodities为空返回null
       if(commodities==null||commodities.size()==0){
           return null;
       }
       //取出该commodity的cid
       Long cid = commodities.get(0).getcId();
       //每次比价时修改商品的hot属性为+1
       //将该commodiy的hot+1，然后将该commodity重新update一下即可
       System.out.println(commodities.size());
       if(commodities.get(0).getHot()==null){
           commodities.get(0).setHot(1);
       }else {
           commodities.get(0).setHot(commodities.get(0).getHot()+1);
       }

       //将commodity主属性设置为null否则更新失败，会出现duplicatePrimaryKey
       commodities.get(0).setId(null);
       //准备updatecommodity的条件
       CommodityExample up_commodity=new CommodityExample();
       CommodityExample.Criteria up_commodity_criteria= up_commodity.createCriteria();
       up_commodity_criteria.andCIdIsNotNull();
       up_commodity_criteria.andCIdEqualTo(cid);
       //根据commodity的cid修改commodity的hot属性
       commodityDao.updateByExampleSelective(commodities.get(0),up_commodity);
       //准备price的查询条件
       //查询price表中p_id为1024的表的数据  SELECT * FROM price WHERE c_id = '1024' ORDER BY c_date DESC LIMIT 0,6;
       PriceExample ce_price=new PriceExample();
       PriceExample.Criteria criteria_price = ce_price.createCriteria();
       criteria_price.andCIdIsNotNull();
       criteria_price.andCIdEqualTo(cid);
       ce_price.setOrderByClause("c_date DESC");
       ce_price.setLimit(pageSize);
       //根据cid查询
       List<Price> historyPrice = priceDao.selectByExample(ce_price);
       //两个信息整合到一个list中
       List<Object> CommodityPriceMsg=new ArrayList<Object>();
       CommodityPriceMsg.add(commodities.get(0));
       CommodityPriceMsg.addAll(historyPrice);
       PageInfo pageInfo=new PageInfo(CommodityPriceMsg);
       return pageInfo;
   }


    public PageResult SearchCommodity(Integer page, Integer limit,String name, String cType) {
        //在查询前进行分页
        //设置起始的分页数
        PageHelper.startPage(page,limit);
        List<Commodity> list = null;
        //判断字符串是否是null
        if(cType == null){
            //执行模糊查询
            list = commodityDao.selectByName(name);
        }else if(name == null){
            list = commodityDao.selectByType(cType);
        }else {
             list = commodityDao.selectByNameAndType(name,cType);
        }
        if(list.size() == 0)
            throw new ClientException("没有此类商品");
        PageInfo<Commodity> pageInfo = new PageInfo<>(list);

        //创建返回值对象
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        //用PageInfo对list进行处理
//        PageInfo<User> pageInfo = new PageInfo<>(list);
        //获取查询的总记录数
        long total = list.size();
        pageResult.setTotal(total);
        return pageResult;
    }

    public String delCommodity(String id) {
       try {
           commodityDao.deleteById(Integer.parseInt(id));
       }catch (Exception e){
           return "false";
       }
       return "ok";
    }

    public String preCommodity(String c_id) {
       //从price表中按照传过来的c_id按照日期进行降序排序
        PriceExample ce_price=new PriceExample();
        PriceExample.Criteria criteria_price = ce_price.createCriteria();
        criteria_price.andCIdIsNotNull();
        criteria_price.andCIdEqualTo(Long.valueOf(c_id));
        ce_price.setOrderByClause("c_date DESC");
        ce_price.setLimit(5);
        //根据cid查询
        List<Price> historyPrice_List = priceDao.selectByExample(ce_price);
        //取出Price对象中的cprice,并将其转换成Int，用数组进行存储
        Double[] price = new Double[5];
        for (int i=0;i<price.length;i++){
            price[i] = Double.parseDouble(historyPrice_List.get(i).getcPrice());
        }
        Double predict_price = 0.3*(price[0]+price[1])+0.2*price[2]+0.1*(price[3]+price[4]);
        return String.format("%.0f",predict_price);
    }

    public PageInfo findTop5Hot() {
        //准备commodity查询条件
        //select * from commodity order by hot desc limit 0,5;
        CommodityExample top5hot_commodity=new CommodityExample();
        top5hot_commodity.setOrderByClause("hot DESC ");
        top5hot_commodity.setLimit(5);
        List<Commodity> commodities = commodityDao.selectByExample(top5hot_commodity);
        PageInfo pageInfo=new PageInfo(commodities);
        return  pageInfo;
    }
}
